using System;
using System.Collections;
using TMPro;
using UnityEngine;
using Utils;

public class Bullet : MonoBehaviour
{
    private AudioSource effectAudio;
    
    public GameObject hitEffect;
    public GameObject bullet;
    public float atk;

    public Rigidbody2D rigidbody2D;
    public Collider2D collider2D;

    public bool rotate = false;
    public float lifeTime = 0;
    public float liveTime = 10.0f;

    // public bool used = false;
    
    public GameObject damageTextGO;

    private void Awake()
    {
        InitUI();
        InitWidget();
    }

    private void OnEnable()
    {
        effectAudio.enabled = GameController.Instance().playerDataCtr.setting.EffectState;
        effectAudio.volume = GameController.Instance().playerDataCtr.setting.EffectValue * 0.01f;
    }

    private void OnDisable()
    {
        // Debug.LogError("???");
        // ResetSelf();
    }

    private void Update()
    {
        RotationEffect();
        if (damageTextGO)
        {
            damageTextGO.transform.position = GameController.Instance().GetMainCamera().WorldToScreenPoint(transform.position);
        }
        
        if ((lifeTime += Time.deltaTime) > liveTime)
        {
            ResetSelf();
            // Destroy(gameObject);
        }
    }

    private void OnDestroy()
    {
        // GameController.Instance().audioCtr.effectAudios.Remove(effectAudio);
    }

    public virtual void InitData(float attack, float bulletSpeed)
    {
        this.atk = attack;
        rigidbody2D.AddForce(transform.right * bulletSpeed, ForceMode2D.Impulse);
    }

    private void InitUI()
    {
        
        bullet = UITool.GetChildGameObject("Bullet", gameObject);
        hitEffect = UITool.GetChildGameObject("HitEffect", gameObject);

        effectAudio = GetComponent<AudioSource>();
        rigidbody2D = GetComponent<Rigidbody2D>();
        collider2D = GetComponent<Collider2D>();
    }

    private void InitWidget()
    {
        GameController.Instance().audioCtr.effectAudios.Add(effectAudio);
    }

    private void OnCollisionEnter2D(Collision2D other)
    {
        print(other.gameObject.name + "[" + other.gameObject.tag + "]--OnCollisionEnter2D");
        if (other.gameObject.tag.Equals("Wall") || other.gameObject.tag.Equals("Reward"))
        {
            DoHitAction();
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        // print(other.gameObject.name + "--OnTriggerEnter2D");
        
        if (other.gameObject.layer.Equals(9) && tag.Equals("Enemy"))
        {
            print(other.transform.name);
            DoHitAction();
            CallDamageText();
            Player player = other.transform.GetComponent<Player>();
            if (player.arm < atk && !player.armImmuneState)
            {
                player.hp -= atk - player.arm;
                player.arm = 0;
            }
            else
            {
                player.arm = Math.Max(player.arm - atk, 0);
            }

            player.BeAttacked();
        }

        if (other.gameObject.tag.Equals("Enemy") && tag.Equals("Player"))
        {
            Monster monster = other.transform.GetComponent<Monster>();
            if (monster)
            {
                DoHitAction();
                CallDamageText();
                monster.hp -= atk;
                monster.BeAttacked();
            }
        }
    }

    private void DoHitAction()
    {
        // Destroy(rigidbody2D);
        rigidbody2D.velocity = Vector2.zero;
        collider2D.enabled = false;
        bullet.SetActive(false);

        hitEffect.SetActive(true);
        effectAudio.Play();
        if (gameObject.activeSelf)
        {
            StartCoroutine(DestroySelf());
        }
    }

    public void ResetSelf()
    {
        lifeTime = 0;
        collider2D.enabled = true;
        bullet.SetActive(true);
        hitEffect.SetActive(false);
        effectAudio.Stop();
        gameObject.SetActive(false);
    }
    
    private IEnumerator DestroySelf()
    {
        yield return new WaitForSeconds(0.3f);
        ResetSelf();
        // Destroy(gameObject);
    }

    private void CallDamageText()
    {
        damageTextGO = UITool.InstantiatePrefabUI(PrefabRoot.DamageText.Path, PrefabRoot.DamageText.Name);
        damageTextGO.GetComponent<TextMeshProUGUI>().text = atk.ToString();
        damageTextGO.SetActive(true);
        damageTextGO.tag = tag;
        Destroy(damageTextGO,0.5f);
    }

    private void RotationEffect()
    {
        if (rotate)
        {
            bullet.transform.Rotate(Vector3.forward);
        }
    }
}

